package com.megvii.koala.tv.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.megvii.corelib.components.BaseNetworkErrorView;
import com.megvii.koala.base.KoalaPresenter;
import com.megvii.koala.tv.R;
import com.megvii.koala.tv.adapter.ThemeAdapter;
import com.megvii.koala.tv.data.ThemeBean;
import com.megvii.koala.tv.presenter.ThemePresenter;

import java.util.List;

import butterknife.Bind;
import butterknife.OnItemClick;

/**
 * Created by linyue on 16/1/25.
 */
public class ThemeFragment extends BaseFragment
{
    protected View rootView;

    @Bind(R.id.theme_table)
    protected GridView themeTable;

    private ThemeAdapter adapter;

    private ThemeListener listener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        if (rootView == null)
        {
            rootView = inflater.inflate(R.layout.theme_layout, container, false);
        }
        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        adapter = new ThemeAdapter(getActivity());
        themeTable.setAdapter(adapter);
    }

    public void updateView(List<ThemeBean> themes)
    {
        adapter.updateDate(themes);
        adapter.notifyDataSetChanged();
    }

    @OnItemClick(R.id.theme_table)
    public void onTableItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        ThemeBean theme = (ThemeBean) parent.getItemAtPosition(position);
        if (listener != null)
        {
            listener.onChooseTheme(theme);
        }
    }

    @Override
    public BaseFragmentListener getBaseFragmentListener()
    {
        return listener;
    }

    @Override
    public Class<? extends KoalaPresenter> getPresenterClass()
    {
        return ThemePresenter.class;
    }

    @Override
    public BaseNetworkErrorView getNetworkErrorView()
    {
        return null;
    }

    public void setListener(ThemeListener listener)
    {
        this.listener = listener;
    }

    public interface ThemeListener extends BaseFragmentListener
    {
        public void onChooseTheme(ThemeBean theme);
    }
}
